﻿define(function (require) {
    require('model/Feed');

    window.FeedList = Backbone.Collection.extend({
        model: Feed,
        comparator: function (e) {
            if (e.get('Type') == "notification")
                return -e.get('UpdateOn') * 2; // notification永远排在前面
            else if (e.get('Type') == "update")
                return -e.get('UpdateOn');
            else
                return 2;
        },
        reset: function (e) {
            var _feedList = this;
            this._reset();
            if (e) {
                this.add(e);
                for (var i in this.models) {
                    var feed = this.models[i];
                    feed.bind('removed', function (cid) {
                        var feedModel = _feedList.getByCid(cid);
                        _feedList.remove(feedModel);
                    });
                    feed.bind('setRead', function (cid) {
                        var unreadFeedCount = _feedList.unread().length;
                        var feedModel = _feedList.getByCid(cid);
                        feedModel.trigger("setFeedRead", unreadFeedCount);
                    });
                }
            }
        },
        unread: function () {
            return this.filter(function (feed) { return !feed.get('IsRead'); });
        }
    });
});